//
//  HomeViewController.swift
//  EY.Mtel_ProjectTemplate_Swift
//
//  Created by zxj on 2020/8/5.
//  Copyright © 2020 EY.Mtel. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {
    var testViewModel:ViewModel?
    var songModelArray: [Song]? = nil {
          didSet {
            tableView.reloadData()
          }
    }
    
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "HOME"
        loadData()
        tableView.separatorColor = UIColor.clear
        let MemberShipCard = UINib(nibName: "HomeTableViewCell", bundle: nil)
        tableView.register(MemberShipCard, forCellReuseIdentifier: NSStringFromClass(HomeTableViewCell.self))
    }
}

extension HomeViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let tempSongModelArry = songModelArray{
             return tempSongModelArry.count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 80
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier:  NSStringFromClass(HomeTableViewCell.self), for: indexPath) as! HomeTableViewCell
        if let tempSongModelArry = songModelArray{
            let model = tempSongModelArry[indexPath.row]
            cell.title.text = String(model.id)
            cell.detaile.text = model.name
            
        }
        return cell
    }
}


extension HomeViewController{
    func loadData(){
        ViewModel.shared.fetchMusicListData(keyword:"刘德华",networkResultClosure: { (Songs) in
            self.songModelArray = Songs
        })
    }

}
